package com.fancon.android.hotgirlcollection;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

import com.fancon.android.activity.FanconBaseActivity;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.rpc.XChannelApiService;
import com.fancon.android.ui.widget.FanconProgressBar;
import com.fancon.android.xchannel.entity.Photo;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class XChannel19PlusActivity extends FanconBaseActivity {
	private int index = 0;
	private View footerView;
	private ListView mGrid;
	private Boolean isFirst = true;
	private boolean loadingMore = false;
	private ArrayList<Photo> listPhotos = new ArrayList<Photo>();
	private Boolean isEnd = false;
	private XChannelAdapter adapter = null;
	private int wallPaper = Menu.FIRST;
	private int setting = Menu.FIRST + 1;
	private int group2Id = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listplaceholder);
		footerView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.listfooter, null, false);
		mGrid = (ListView) findViewById(R.id.x_list);
		mGrid.addFooterView(footerView);
		// Load ads
		AdView adview = (AdView) findViewById(R.id.adView);
		AdRequest re = new AdRequest();
		//re.setTesting(true);
		re.setGender(AdRequest.Gender.FEMALE);
		adview.loadAd(re);

		mGrid.setOnScrollListener(new OnScrollListener() {

			// useless here, skip!
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			// dumdumdum
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

				// what is the bottom iten that is visible
				int lastInScreen = firstVisibleItem + visibleItemCount;

				// is the bottom item visible & not loading more already ? Load
				// more !
				if ((lastInScreen == totalItemCount) && !(loadingMore)
						&& (!isEnd) && (!isFirst)) {
					loadMore();
				}
			}
		});
		footerView.setVisibility(View.GONE);
		load();
	}

	public void loadMore() {
		index++;
		load();
	}

	public void load() {
		UserPhotoRequest photoReq = new UserPhotoRequest("photo_rq", this);
		getGlobalState().getRequestQueue().addRequest(photoReq);
	}

	private void loadDataView() {
		if (!isEnd) {
			footerView.setVisibility(View.VISIBLE);
		} else {
			footerView.setVisibility(View.GONE);
		}
		if (adapter == null) {
			adapter = new XChannelAdapter(this, listPhotos);
			mGrid.setAdapter(adapter);
		} else {
			// adapter.setLisPhoto(listPhotos);
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(group2Id, wallPaper, wallPaper,
				getResources().getString(R.string.about)).setIcon(
				R.drawable.ic_menu_about);
		// menu.add(group2Id, setting, setting,
		// getResources().getString(R.string.set_pass)).setIcon(
		// R.drawable.ic_pass);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST:
			doAbout();
			break;
		case Menu.FIRST + 1:
			doSetPass();
			break;
		}
		return true;
	}

	private void doAbout() {
		try {
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse("market://search?q=pub:Fancon+Software"));
			startActivity(intent);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private void doSetPass() {

	}

	class UserPhotoRequest extends RequestUI {
		private Activity context;
		private FanconProgressBar progress;
		private ArrayList<Photo> tmp;

		public UserPhotoRequest(Object key, Activity activity) {
			super(key, activity);
			// TODO Auto-generated constructor stub
			loadingMore = true;
			context = activity;
			if (isFirst) {
				progress = FanconProgressBar.show(context,
						context.getString(R.string.common_loading));
			}
		}

		@Override
		public void execute() throws Exception {
			XChannelApiService api = new XChannelApiService(context);
			tmp = api.getPhotos(index);
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			if (progress != null) {
				progress.dismiss();
			}
			try {
				if (tmp.size() > 0) {
					if (listPhotos.size() > 0 && !isFirst) {
						listPhotos.addAll(tmp);
					} else {
						listPhotos = tmp;
					}
				}
				if (tmp.size() == 0) {
					isEnd = true;
				}
			} catch (Exception e) {
				// TODO: handle exception
				isEnd = true;
				e.printStackTrace();
			}
			loadDataView();
			loadingMore = false;
			isFirst = false;
		}

	}
}
